package com.example.studentdemo.init;

import com.example.studentdemo.entity.Admin;
import com.example.studentdemo.service.AdminService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import jakarta.annotation.Resource;

@Component
public class AdminInit implements CommandLineRunner {

    @Resource
    private AdminService adminService;

    @Override
    public void run(String... args) throws Exception {
        // 检查是否存在管理员账号
        Admin admin = adminService.selectByUsername("admin");
        if (admin == null) {
            // 创建初始管理员账号
            admin = new Admin();
            admin.setUsername("admin");
            admin.setPassword("admin123"); // 初始密码
            admin.setName("超级管理员");
            adminService.add(admin);
            System.out.println("初始管理员账号创建成功：用户名admin，密码admin123");
        } else {
            System.out.println("管理员账号已存在，无需创建");
        }
    }
}